package com.zhuangjie.rider.controller;

import com.zhaungjie.base.conduit.R;
import com.zhuangjie.base.constant.ModuleSecurityNames;
import com.zhuangjie.base.dto.page.PageDTO;
import com.zhuangjie.base.vo.PageVO;
import com.zhuangjie.mvc.helper.GeoHelper.GeoCoordinates;
import com.zhuangjie.rider.dto.CreateDeliveryOrderDTO;
import com.zhuangjie.rider.dto.NearbyDeliveryOrderQueryDTO;
import com.zhuangjie.rider.service.DeliveryOrderService;
import com.zhuangjie.rider.vo.CompletedQuantityStatisticsVO;
import com.zhuangjie.rider.vo.DeliveryOrderDetailsVO;
import com.zhuangjie.rider.vo.DeliveryOrderForUserOrderMapVO;
import com.zhuangjie.rider.vo.NearbyDeliveryOrder;
import com.zhuangjie.security.annotations.OnlySystemCall;
import com.zhuangjie.security.annotations.PublicAPI;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.Collection;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhuangjie
 * @since 2023-12-26
 */
@RestController
@RequestMapping("/delivery-order")
public class DeliveryOrderController {
    @Autowired
    private DeliveryOrderService deliveryOrderService;

    @PostMapping("/submit")
    @ApiOperation("创建配送单")
    @PublicAPI(modules = {ModuleSecurityNames.SHOP})
    @OnlySystemCall
    public R submit(@Valid @RequestBody CreateDeliveryOrderDTO createDeliveryOrderDTO) {
        return R.capture(() -> deliveryOrderService.tryCreateDeliveryOrder(createDeliveryOrderDTO));
    }

    @GetMapping("/nearby-delivery-orders")
    @ApiOperation("获取骑手附近的配送单列表")
    public R nearbyDeliveryOrders(@ModelAttribute NearbyDeliveryOrderQueryDTO nearbyDeliveryOrderQueryDTO) {
        PageVO<NearbyDeliveryOrder> nearbyDeliveryOrders = deliveryOrderService.findNearbyDeliveryOrders(nearbyDeliveryOrderQueryDTO);
        return R.require(PageVO.class, nearbyDeliveryOrders);
    }

    @PostMapping("/accept-system-dispatch-delivery-order/{deliveryOrderId}")
    @ApiOperation("接单系统派单")
    public R acceptSystemDispatchDeliveryOrder(@PathVariable("deliveryOrderId") Long deliveryOrderId) {
        return R.capture(() -> deliveryOrderService.acceptSystemDispatchDeliveryOrder(deliveryOrderId));
    }

    @PostMapping("/try-grabbing-order/{deliveryOrderId}")
    @ApiOperation("骑手抢单")
    public R grabbingOrder(@PathVariable("deliveryOrderId") Long deliveryOrderId) {
        return R.capture(() -> deliveryOrderService.grabbingOrder(deliveryOrderId));
    }

    @PostMapping("/report-to-shop/{deliveryOrderId}")
    @ApiOperation("骑手上报到达店铺")
    public R reportToShop(@PathVariable("deliveryOrderId") Long deliveryOrderId) {
        return R.require(Integer.class, deliveryOrderService.reportToShop(deliveryOrderId));
    }

    @PostMapping("/rider-complete-delivery/{deliveryOrderId}")
    @ApiOperation("骑手点击送达")
    public R riderCompleteDelivery(@PathVariable("deliveryOrderId") Long deliveryOrderId) {
        return R.capture(() -> deliveryOrderService.riderCompleteDelivery(deliveryOrderId));
    }

    @PostMapping("/confirm-meal-pickup/{deliveryOrderId}")
    @ApiOperation("确认取餐")
    public R confirmMealPickup(@PathVariable("deliveryOrderId") Long deliveryOrderId) {
        return R.capture(() -> deliveryOrderService.confirmMealPickup(deliveryOrderId));
    }

    @GetMapping("/rider-wait-picking-page")
    @ApiOperation("获取骑手待取餐列表")
    public R riderWaitPickingPage(@ModelAttribute PageDTO pageDTO) {
        return R.require(PageVO.class, deliveryOrderService.riderWaitPickingPage(pageDTO));
    }

    @GetMapping("/rider-wait-delivery-page")
    @ApiOperation("获取骑手待派送列表")
    public R riderWaitDelivery(@ModelAttribute PageDTO pageDTO) {
        return R.require(PageVO.class, deliveryOrderService.riderWaitDeliveryPage(pageDTO));
    }

    @GetMapping("/rider-route-planning")
    @ApiOperation("获取骑手路径规划所需数据")
    public R riderRoutePlanning(@ModelAttribute GeoCoordinates riderGeo) {
        return R.require(Collection.class, deliveryOrderService.riderRoutePlanning(riderGeo));
    }

    @GetMapping("/delivery-order-detail/{deliveryOrderId}")
    @ApiOperation("获取配送单详情")
    public R deliveryOrderDetail(@PathVariable("deliveryOrderId") Long deliveryOrderId, @ModelAttribute GeoCoordinates riderGeo) throws Exception {
        return R.require(DeliveryOrderDetailsVO.class, deliveryOrderService.deliveryOrderDetail(deliveryOrderId, riderGeo));
    }

    @GetMapping("/completed-quantity-statistics")
    @ApiOperation("查询一段时间内完成的量统计")
    public R completedQuantityStatistics(@RequestParam(value = "startTime", required = false) LocalDateTime startTime, @RequestParam(value = "endTime", required = false) LocalDateTime endTime) {
        return R.require(CompletedQuantityStatisticsVO.class, deliveryOrderService.completedQuantityStatistics(startTime, endTime));
    }


    @GetMapping("/delivery-info-for-user-order-map/{orderId}")
    @ApiOperation("获取配送信息")
    @PublicAPI
    @OnlySystemCall
    public R deliveryInfoForUserOrderMap(@PathVariable("orderId") Long orderId) {
        return R.ok().data(DeliveryOrderForUserOrderMapVO.class, deliveryOrderService.deliveryInfoForUserOrderMap(orderId));
    }




}
